package com.example.spring_secutity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.spring_secutity.entity.SecurityUser;
import com.example.spring_secutity.entity.User;
import com.example.spring_secutity.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.Arrays;


@Component
public class DBUserDetailsService implements UserDetailsService {

    @Resource
    IUserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usr_name",username);
        User user = userService.getOne(queryWrapper);
        System.out.println("---》》user："+user);
        if(null == user){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        return  new SecurityUser(user, Arrays.asList("USER"));//new InMemoryUserDetailsManager(user);;
    }
}
